package com.imooc.dianping.service.impl;

import com.imooc.dianping.common.BusinessException;
import com.imooc.dianping.common.EmBusinessError;
import com.imooc.dianping.dal.SellerModelMapper;
import com.imooc.dianping.model.SellerModel;
import com.imooc.dianping.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author LiJian
 * @date 2021/7/14
 */
@Service
public class SellerServiceImpl implements SellerService {
    @Autowired
    private SellerModelMapper sellerModelMapper;
    @Override
    @Transactional
    public SellerModel create(SellerModel sellerModel) {
        Date now = new Date();
        sellerModel.setCreateAt(now);
        sellerModel.setUpdateAt(now);
        sellerModel.setRemarkScore(new BigDecimal(0));
        sellerModel.setDisabledFlag(0);
        this.sellerModelMapper.insertSelective(sellerModel);
        return this.get(sellerModel.getId());
    }

    @Override
    public SellerModel get(Integer id) {
        return this.sellerModelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SellerModel> selectAll() {
        return sellerModelMapper.selectAll();
    }

    @Override
    public SellerModel changeStatus(Integer id, Integer disableFlag) throws BusinessException {
        SellerModel sellerModel = this.get(id);
        if(null == sellerModel){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        sellerModel.setDisabledFlag(disableFlag);
        this.sellerModelMapper.updateByPrimaryKeySelective(sellerModel);
        return sellerModel;
    }

    @Override
    public Integer countAllSeller() {
        return this.sellerModelMapper.countAllSeller();
    }
}
